/////////////////////////////////////////////////////////////////////////

package sheep.view;

/////////////////////////////////////////////////////////////////////////

import java.util.LinkedList;
import sheep.view.graphics2d.LinearGradientTypeLoader;
import sheep.utils.fileio.Bundles;
import org.jdesktop.fuse.ResourceInjector;
import org.jdesktop.fuse.TypeLoaderFactory;
import org.jdesktop.fuse.swing.SwingModule;

/////////////////////////////////////////////////////////////////////////
import sheep.controller.Workspace;

// Gestionnaire de transitions
import sheep.model.Task;
import sheep.utils.fileio.WorkspaceAutoLoader;
public class TransitionManager {

  /////////////////////////////////////////////////////////////////////////
  
    // flag qui definit si une transition peut etre effectuee
    // (pas deja une en cours)
    static boolean ready = false;
    //Objeet de synchronization
    static final Object LOCK = new Object();
    
    // Mainframe et ses 2 principales zones
    private static MainFrame mainFrame;
    // La barre de navig
    private static NavigationHeader navPanel;
    // La zone inferieure
    private static TransitionPanel transPanel;
    
    private static Workspace workSpace= new Workspace();


 
    /////////////////////////////////////////////////////////////////////////
    
    // Effectue les transitions
    private TransitionManager() {}
    
    /////////////////////////////////////////////////////////////////////////
    
    public static MainFrame createMainFrame() {
        // En creant la frame principale, on recupere le fichier de thèmes
        // Thanks Fuse !!!
        ResourceInjector.addModule(new SwingModule());
        ResourceInjector.get().load(MainFrame.class,
                                    "/resources/" +
                                    "photos" +
                                    ".uitheme");
        //
        TypeLoaderFactory.addTypeLoader(new LinearGradientTypeLoader());
        // création des Panels
        navPanel = new NavigationHeader(workSpace);
        transPanel = new TransitionPanel(navPanel,workSpace);
        mainFrame = new MainFrame(transPanel);
        
        // On charge le workspace dernierement ouvert
        WorkspaceAutoLoader wl = new WorkspaceAutoLoader(workSpace);
        wl.loadWorkspace();
        transPanel.refreshAfterLoad();
        
        // On affiche l'intro
        showIntroduction();

        return mainFrame;
    }
 
    
    static MainFrame getMainFrame() {
        return mainFrame;
    }
    
    // Affiche la zone principal de travail
   public static void showTransitionPanel() {
        mainFrame.showTransitionPanel();
    }
    
    // Ecran d'into (le mouton)
   public static void showIntroduction() {
        mainFrame.showIntroduction();
    }

   public static void refreshGraph(){
     if (workSpace.getCurrentTask()!=null){
        transPanel.refreshGraph();
        mainFrame.repaint();
     }
   }
      
    // Affichage de l'image en surimpression au départ
    static void showWaitOverlay() {

     mainFrame.showWaitOverlay();
     TransitionManager.hideWaitOverlay();
 
     TransitionManager.showMainScreen();
    }
    
    static void hideWaitOverlay() {
        mainFrame.hideWaitOverlay();
    }
    
    static void killOverlay() {
        mainFrame.killOverlay();
    }
    
    // Ecran d'accueil
   public static void showMainScreen() {
      if (!Bundles.getMessage(TransitionManager.class, "TXT_Lobby").equals(navPanel.getLastBttn())){
        MainPanel mainPanel = transPanel.getMainPanel();
        mainPanel.setOperations();
        mainPanel.setRandomPicks();
        navPanel.clearLinks();
        navPanel.addLink(Bundles.getMessage(TransitionManager.class, "TXT_Lobby"));        
      }
      transPanel.showMainPanel();
    }
    
    // Ecran projet
    public static void showMainPoject() {
       if (!Bundles.getMessage(TransitionManager.class, "TXT_Project").equals(navPanel.getLastBttn())){
          ProjectPanel projectPanel = transPanel.getMainProjectPanel();
          projectPanel.setTasks();
          projectPanel.setRandomPicks();
          navPanel.clearLinks();
          navPanel.addLink(Bundles.getMessage(TransitionManager.class, "TXT_Lobby"));
          navPanel.addLink(Bundles.getMessage(TransitionManager.class, "TXT_Project"));
       }
       transPanel.showMainProjectPanel();
    }
  
          // Ecran Agenda semaine
   public static void showUser() {
      // On verifie que Agenda n'est pas déja ajouter à la barre de navigu.
      if (!("User").equals(navPanel.getLastBttn())){
        navPanel.clearLinks();
        navPanel.addLink(Bundles.getMessage(TransitionManager.class, "TXT_Lobby"));
        navPanel.addLink("User");
      }
      transPanel.showUserPanel();
    }
    
     // Ecran Agenda semaine
   public static void showAgenda() {
      // On verifie que Agenda n'est pas déja ajouter à la barre de navigu.
      if (!("Agenda").equals(navPanel.getLastBttn())){
        navPanel.clearLinks();
        navPanel.addLink(Bundles.getMessage(TransitionManager.class, "TXT_Lobby"));
        navPanel.addLink("Agenda");
      }
      transPanel.showAgendaPanel();
    }
    
      
   // Affichege de l'editeur de graphe
  public static void showProjectGraphEdit() {
     if (workSpace.getCurrentTask()!=null){
        navPanel.clearLinks();
        navPanel.addLink(Bundles.getMessage(TransitionManager.class, "TXT_Lobby"));
        navPanel.addLink(Bundles.getMessage(TransitionManager.class, "TXT_Project"));
        navPanel.addLink("Editeur");
        LinkedList <Task> list = new LinkedList<Task>();
        Task t= workSpace.getCurrentTask();
        list.add(t);
        while (t.getSuperTask()!=null){         
          t= t.getSuperTask();
          list.add(t);
        }
        if (list.size()>0)
          for (int i= list.size()-1; i >=0; i--)
            navPanel.addLink(list.get(i).getName());
        
        transPanel.showVisualProjectPanel();
     }
    }
 
       // Affichege de l'editeur de graphe
  public static void showEdit() {
     if (workSpace.getCurrentTask()!=null){
        navPanel.clearLinks();
        navPanel.addLink(Bundles.getMessage(TransitionManager.class, "TXT_Lobby"));
        navPanel.addLink(Bundles.getMessage(TransitionManager.class, "TXT_Project"));
        navPanel.addLink("Editeur");
        LinkedList <Task> list = new LinkedList<Task>();
        Task t= workSpace.getCurrentTask();
        list.add(t);
        while (t.getSuperTask()!=null){         
          t= t.getSuperTask();
          list.add(t);
        }
        if (list.size()>0)
          for (int i= list.size()-1; i >=0; i--)
            navPanel.addLink(list.get(i).getName());
        
        transPanel.showEditPanel();
     }
    }
  
      // Affichege de l'editeur de graphe
  public static void showProjectEdit() {
     if (!("Editeur").equals(navPanel.getLastBttn())){
        navPanel.clearLinks();
        navPanel.addLink(Bundles.getMessage(TransitionManager.class, "TXT_Lobby"));
        navPanel.addLink(Bundles.getMessage(TransitionManager.class, "TXT_Project"));
        navPanel.addLink("Editeur");
        transPanel.showProjectEditPanel();
     }
    }

        // Affichege de l'editeur de graphe
  public static void showGanttEdit() {
     if (workSpace.getCurrentTask()!=null){
     if (workSpace.getCurrentTask()!=null){
        navPanel.clearLinks();
        navPanel.addLink(Bundles.getMessage(TransitionManager.class, "TXT_Lobby"));
        navPanel.addLink(Bundles.getMessage(TransitionManager.class, "TXT_Project"));
        navPanel.addLink("Editeur");
        LinkedList <Task> list = new LinkedList<Task>();
        Task t= workSpace.getCurrentTask();
        list.add(t);
        while (t.getSuperTask()!=null){         
          t= t.getSuperTask();
          list.add(t);
        }
        if (list.size()>0)
          for (int i= list.size()-1; i >=0; i--)
            navPanel.addLink(list.get(i).getName());
        
          transPanel.showGanttPanel();
       }
     }
    }
  
   // Gestion de la toolbar
  public static void showToolBar() {
       if (transPanel.getToolBarCollapser().isValid())
        transPanel.getToolBarCollapser().setCollapsed(true);
    }
   
   static boolean isToolBarDisplay() {
        return transPanel.getToolBarCollapser().isCollapsed();
    }
      static void hideToolBar() {
        if (transPanel.getToolBarCollapser().isValid())
            transPanel.getToolBarCollapser().setCollapsed(false);
    }
    /////////////////////////////////////////////////////////////////////////
    
}

/////////////////////////////////////////////////////////////////////////